layui.use(['layer', 'form', 'table', 'ztree', 'laydate', 'admin', 'ax'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;

    var layer = layui.layer;
    var $ZTree = layui.ztree;
    var laydate = layui.laydate;

    /**
     * 收货单位表管理
     */
    var BasicPutgoods = {
        tableId: "basicPutgoodsTable",    //表格id
        condition: {
            dictId: "",
        }
    };

    /**
     * 往来地址表管理
     */
    var BasicAddress = {
        tableId: "basicAddressTable"
    };

    /**
     * 初始化表格的列
     */
    BasicPutgoods.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'putgoodsName', title: '单位名称', style:'font-weight: 600;'},
            {field: 'unitNumber', title: '单位编号', style:'font-weight: 600;'},
            {field: 'unitType', title: '单位类型'},
        ]];
    };

    /**
     * 选择时
     */
    BasicPutgoods.onClickDept = function (e, treeId, treeNode) {
        BasicPutgoods.condition.dictId = treeNode.id;
        BasicPutgoods.search();
    };

    /**
     * 点击查询按钮
     */
    BasicPutgoods.search = function () {
        var queryData = {};
        queryData['dictId'] = BasicPutgoods.condition.dictId;
        queryData['condition2'] = $("#condition").val();

        queryData['unitType'] = $("#unitType option:selected").html();
        table.reload(BasicPutgoods.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BasicPutgoods.tableId,
        url: Feng.ctxPath + '/basicPutgoods/list_hx',
        page: true,
        limit: 60,
		limits:[60,120,240,360,480,600],
        height: "full-60",
        cellMinWidth: 100,
        cols: BasicPutgoods.initColumn()
    });
    var selectedDataForSearch = [];
    table.on('checkbox(basicPutgoodsTable)', function(obj){
        var data = obj.data; // 被选中的数据
        if(obj.type === 'all'){ // 全选
            if(obj.checked){ // 全选时，向已选择数据数组中添加所有数据
                selectedDataForSearch = table.cache['basicPutgoodsTable'];
            }else{ // 全部取消选中时，清空已选择数据数组
                selectedDataForSearch = [];
            }
        }else{ // 单选
            if(obj.checked){ // 单个选中，向已选择数据数组中添加该条数据
                selectedDataForSearch.push(data);
            }else{ // 单个取消选中，从已选择数据数组中移除该条数据
                selectedDataForSearch = selectedDataForSearch.filter(function(item) {
                    return item.id !== data.id;
                });
            }
        }
    });

    $('#assign').click(function () {
    	var checkStatus = table.checkStatus('basicPutgoodsTable');
        data = checkStatus.data;
        if (data.length>0) {
            var ids = [];
            var putgoodsNames = [];
            for (var i = 0; i < data.length; i++) {
            	// if(i > 0){
            	// 	ids+=",";
            	// 	putgoodsNames+=",";
            	// }
                ids.push(data[i].id);
                putgoodsNames.push(data[i].putgoodsName);
			}

            selectedDataForSearch = selectedDataForSearch.concat(putgoodsNames);
            localStorage.setItem('selectedData', JSON.stringify(selectedDataForSearch));
            parent.BasicPutgoodsInfoDlg.data.id = ids.join(",");
            // parent.BasicPutgoodsInfoDlg.data.putgoodsName = putgoodsNames.join(",");
            $(document).ready(function() {
                var storedData = localStorage.getItem('selectedData');
                if (storedData) {
                    selectedDataForSearch = JSON.parse(storedData);
                    // 回填已选择的数据到搜索框中
                    var selectedNames = selectedDataForSearch.map(function(item) {
                        return item.putgoodsName;
                    });
                    parent.BasicPutgoodsInfoDlg.data.putgoodsName = selectedNames.join(",");

                    $("#condition").val(selectedNames.join(","));
                }
            });
            var Index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(Index);
		} else {
			layer.alert('请至少选中一条数据');
		}
    });


    //回填商品数据
    function confirm(data){
    	parent.BasicPutgoodsInfoDlg.data.id = data.id;
    	parent.BasicPutgoodsInfoDlg.data.putgoodsName = data.putgoodsName;
    	var Index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(Index);
    }
    
    // 双击行事件
    table.on('rowDouble(' + BasicPutgoods.tableId + ')', function (obj) {
        var data = obj.data;
    	confirm(data);
    });

    //初始化左侧部门树
    var ztree = new $ZTree("deptTree", "/hxBasicUnitCode/treeo?v1="+$("#unitType").val());
    ztree.bindOnClick(BasicPutgoods.onClickDept);
    ztree.init();

	//选择单位类型时
    form.on('select(unitType)',function(){
	    //初始化左侧部门树
	    var ztree = new $ZTree("deptTree", "/hxBasicUnitCode/treeo?v1="+$("#unitType").val());
	    ztree.bindOnClick(BasicPutgoods.onClickDept);
	    ztree.init();

        BasicPutgoods.condition.dictId = "";
        BasicPutgoods.search();
		
    	form.render('select');
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BasicPutgoods.search();
    });

    //返回按钮
    $("#backupPage").click(function () {
    	var Index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(Index);
    });
    
});
